const app = getApp()
// 在页面中定义插屏广告
let interstitialAd = null
import {
  myRequesy,
} from '../../request/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight, //获取高度
    // 头部选项卡数组
    head: [{
        name: "比赛介绍",
        status: true,
        id: 0
      },
      {
        name: "上届优胜作品展",
        status: false,
        id: 1
      }
    ],
    megaGameObj: null //数据列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.fid) {
      this.setData({
        fid: options.fid
      })
    }

    this.getMegegameList()

    // 在页面onLoad回调事件中创建插屏广告实例
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: 'adunit-83dea11664d2caa2'
      })
      interstitialAd.onLoad(() => {})
      interstitialAd.onError((err) => {})
      interstitialAd.onClose(() => {})
    }

    // 在适合的场景显示插屏广告
    if (interstitialAd) {
      interstitialAd.show().catch((err) => {
        console.error(err)
      })
    }
  },

  //获取数据列表
  getMegegameList(id) {
    let that = this
    myRequesy({
      url: 'big-activity/detail',
      data: {
        fid: that.data.fid ? that.data.fid : 0
      }
    }).then(res => {
      if (res.data.code === 0) {
        that.setData({
          megaGameObj: res.data.data
        })
      }
    })
  },

  // 顶部tab切换区域
  headleTab(e) {
    let {
      index
    } = e.currentTarget.dataset
    let {
      head
    } = this.data
    head.forEach((item, i) => i === index ? item.status = true : item.status = false)
    this.setData({
      head
    })
  },


  //点击用户授权的回调
  getRecommendLogin() {
    let that = this
    wx.getUserProfile({
      desc: '获取您的用户名和头像',
      success(res1) {
        console.log(res1);
        wx.login({
          success(res2) {
            console.log(res2);
            let userData = {
              code: res2.code,
              nickName: res1.userInfo.nickName,
              avatarUrl: res1.userInfo.avatarUrl
            }
            myRequesy({
              url: 'Login',
              data: userData,
              method: 'POST'
            }).then(result => {
              wx.setStorage({
                data: result.data.data,
                key: 'userInfo',
                success() {
                  that.getMegegameList()
                }
              })
            })
          }
        })
      }
    })
  },

  // 返回上一页
  headleBack() {
    let page = getCurrentPages()
    if (page.length > 1) {
      wx.navigateBack()
      return
    }
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  // 跳转活动报名页
  headleNavApply() {
    let that = this
    let {
      megaGameObj,
      fid
    } = this.data
    if (megaGameObj.state === 1) {
      this.getRecommendLogin()
    } else if (megaGameObj.state === 2) {
      wx.navigateTo({
        url: '/pages/apply/apply?banner=' + megaGameObj.banner + '&sign_time=' + megaGameObj.sign_time + '&name=' + megaGameObj.title + '&tagId=' + megaGameObj.id + '&web_view=' + megaGameObj.web_view + '&status=3' + '&fid=' + fid,
      })
    } else {
      let data = encodeURIComponent(that.data.megaGameObj.web_view) //编码
      wx.navigateTo({
        url: '/pages/webView/webView?url=' + data,
      })
    }

  },

  //  跳转到对应的公众号
  headleNav(e) {
    console.log(e);

    let {
      url
    } = e.currentTarget.dataset
    let data = encodeURIComponent(url) //编码
    wx.navigateTo({
      url: '/pages/webView/webView?url=' + data,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMegegameList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})